package com.site_assembly.controller;

import com.site_assembly.common.JWTHelper;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@Slf4j
public class Test {

    @Resource
    private JWTHelper jwtHelper;


    @GetMapping("login")
    public Integer login() {

        Date expire = DateUtils.addDays(new Date(), 1000000);
        Map<String, Object> payload = new HashMap<>();
        payload.put("param",1);
        String jwt = jwtHelper.createJWT(expire, payload);
        log.info("颁发的jwt为:{}",jwt);
        HttpServletResponse response =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        response.setHeader("auth_token",jwt);

        return 1;

    }

    @Resource
    private HttpServletRequest request;

    @GetMapping("cc")
    public Integer getId(String jwt) {

        String param = request.getHeader("auth_token");

        Claims token = jwtHelper.parseJWT(param);

        Object param1 = token.get("param");
        System.out.println(param1);


        return 1;

    }
}
